﻿#include "pch.h"
#pragma comment(lib,"urlmon.lib")

int _Wo_Files_FileEnums(CXTextA dirName, CXVector<CXTextA>& rFileList, CXTextA fileName, bool addPath, bool enumSon,const char*fPath) {
#ifdef _WIN32
    if (!enumSon) { rFileList.clear(); }
    if (dirName == "") { return 0; }
    CXTextA lDir = dirName;
    if (lDir[lDir.size() - 1] != '\\') { lDir += '\\'; }
    CXVector<CXTextA> lpfx = { "*.*" };
    int lpfxNum = lpfx.size();
    for (int i = 0; i < lpfxNum; i++) {
        WIN32_FIND_DATAA FindData;
        CXTextA l_name = lDir + lpfx[i];
        HANDLE hFind = FindFirstFileA(l_name, &FindData);
        do {
            if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0) {
                continue;
            }
            if (FindData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) {
                //file
                l_name = FindData.cFileName;
                if (addPath) { 
                    l_name = lDir + l_name; 
                }
                else {
                    l_name = fPath + std::string("/");
                    l_name += FindData.cFileName;
                }
                rFileList.add(l_name);
            }
        } while (FindNextFileA(hFind, &FindData));
        FindClose(hFind);
    }
    if (enumSon) {
        WIN32_FIND_DATAA FindData;
        CXTextA l_name = lDir + "*.*";
        HANDLE hFind = FindFirstFileA(l_name, &FindData);
        do {
            if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0) {
                continue;
            }
            if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
                l_name = lDir + FindData.cFileName;
                std::string fp = fPath;
                fp += "/";
                fp += FindData.cFileName;
                _Wo_Files_FileEnums(l_name, rFileList, fileName, addPath, enumSon, fp.c_str());
            }
        } while (FindNextFileA(hFind, &FindData));
        FindClose(hFind);
    }
    return rFileList.size();
#endif // _WIN32
    return 0;
}

CXTextA _Wo_Files_GetFileMIMEType(const char* fileName) {
#ifdef _WIN32
	LPWSTR pwzMimeOut = NULL;
	auto hr = FindMimeFromData(NULL, A2W_(fileName), NULL, 0, NULL, FMFD_URLASFILENAME, &pwzMimeOut, 0);
	if (hr == S_OK && pwzMimeOut != NULL) {
		CXTextA r = W2A_(pwzMimeOut);
		CoTaskMemFree(pwzMimeOut);
		return r;
	}
#endif
    return "";
}